<?php
/*************************************************

用户协议主控制器 - the
Author: WangHuabin  <17-6-26>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class ActiveController extends CommonController {
	public function index(){
		if(IS_POST){
			$data['name'] = I('post.name');
			$data['phone'] = I('post.phone');
			$data['company'] = I('post.company');
			$data['industry']= I('post.industry');
			$data['add_time']= time();
			$r = M('Signup')->data($data)->add();
			if($r){
				$data = array('msg'=>'提交成功！','err'=>0);
			}else{
				$data = array('msg'=>'提交失败！','err'=>1);
			}
			$this->ajaxReturn($data);
		}else{
			 $signPackage = $this->getSignPackage(); 
			 $this->assign('signPackage',$signPackage);
			$this->display();
		}
	}
	
	

     public function getSignPackage() {

    $jsapiTicket = $this->getJsApiTicket();

    $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    $timestamp = time();

    $nonceStr = $this->createNonceStr();

    // 这里参数的顺序要按照 key 值 ASCII 码升序排序

    $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

    $signature = sha1($string);

    $signPackage = array(

      "appId"     =>'wxc3926d97e0dbcf4e',

      "nonceStr"  => $nonceStr,

      "timestamp" => $timestamp,

      "url"       => $url,

      "signature" => $signature,

      "rawString" => $string

    );

    return $signPackage; 

  }

  public function createNonceStr($length = 16) {

    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    $str = "";

    for ($i = 0; $i < $length; $i++) {

      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);

    }

    return $str;

  }

  public function getJsApiTicket() {

    // jsapi_ticket 应该全局存储与更新，以下代码以写入到文件中做示例

    $data = json_decode(file_get_contents("jsapi_ticket.json"));

    if ($data->expire_time < time()) {

      $accessToken = $this->getAccessToken();

      $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";

      $res = json_decode($this->httpGet($url));

      $ticket = $res->ticket;

      if ($ticket) {

        $data->expire_time = time() + 7000;

        $data->jsapi_ticket = $ticket;

        $fp = fopen("jsapi_ticket.json", "w");

        fwrite($fp, json_encode($data));

        fclose($fp);

      }

    } else {

      $ticket = $data->jsapi_ticket;

    }

    return $ticket;

  }

  public function getAccessToken() {

    // access_token 应该全局存储与更新，以下代码以写入到文件中做示例

    $data = json_decode(file_get_contents("access_token.json"));

    if ($data->expire_time < time()) {

      $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc3926d97e0dbcf4e&secret=41650eab872ae4d4ef897deb1434c074";

      $res = json_decode($this->httpGet($url));
      //print_r($res);die;
      $access_token = $res->access_token;

      if ($access_token) {

        $data->expire_time = time() + 7000;

        $data->access_token = $access_token;

        $fp = fopen("access_token.json", "w");

        fwrite($fp, json_encode($data));

        fclose($fp);

      }

    } else {

      $access_token = $data->access_token;

    }

    return $access_token;

  }

  public function httpGet($url) {

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($curl, CURLOPT_TIMEOUT, 500);

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);

    curl_close($curl);

    return $res;

  }
  

}